<%--
  @author Supun Viraj Rathnayaka
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<script src="${pageContext.request.contextPath}/js/printer.js" type="text/javascript"></script>

<script type="text/javascript">
    $(function () {
        $('.land-table').html(initTableHead());

        $('select#railwayTrackId').bind('change', function (evt1) {
            var id = $("select#railwayTrackId").attr("value");
            if(id>0){
                $.getJSON('${pageContext.request.contextPath}/common/DivisionLookupService', {id: id, mode: 6},
                        function (data) {
                            var options1 = '<option value="0">' + $('#all').val() + '</option> ';
                            var ds = data.districtList;
                            for (var i = 0; i < ds.length; i++) {
                                options1 += '<option value="' + ds[i].optionValue + '">' + ds[i].optionDisplay + '</option>'
                            }
                            $("select#districtId").html(options1);
                        });
            }else{
                $("select#districtId").html('<option value="0">' + $('#all').val() + '</option> ');
            }
        });
    });


    function generateStaicstics() {
        var type = "arrearsList"
        var branchId = $('select#branchId').attr("value");
        var trackId  = $('select#railwayTrackId').attr("value");
        var districtId = $('select#districtId').attr("value");
        var minAmnt = $('input#arrearsMinId').val();
        var maxAmnt = $('input#arrearsMaxId').val();

        $.getJSON('${pageContext.request.contextPath}/common/RailwayReportService', {type: type, branch: branchId, track: trackId, district: districtId, minAmnt: minAmnt, maxAmnt: maxAmnt},function (data) {
            if(data){
                loadLandList(data.arrearsList);
            }else{
                $('.land-table').html(initTableHead());
            }

        });
    }

    function loadLandList(landInfo) {
        var opt = '';
        opt += initTableHead();
        opt += '<tbody>';
        var l ;
        if (landInfo.length > 0) {
            for (var i = 0; i < landInfo.length; i++) {
                l = landInfo[i];
                var a_links= '<a title="View" href="loadRailwayLandDetails.do?landId=' + l.land_id + '"> <img src="${pageContext.request.contextPath}/images/d-view.png" style="border:none; width:25px; height:25px;"/> </a>'+
                             // Issue Notification Icon is do nothing now. This is tobe used in future enhancements
                             '<a title="Issue Notification" href="#"> <img src="${pageContext.request.contextPath}/images/notify-letter.png" style="border:none; width:25px; height:25px;"/> </a>';
                opt += '<tr>' +
                        '<td class="center-align">' + l.fileNo + '</td>' +
                        '<td class="center-align">' + l.referenceNo + '</td>' +
                        '<td class="center-align">' + l.valuationAmount + '</td>' +
                        '<td class="center-align">' + l.annualRental + '</td>' +
                        '<td class="center-align">' + l.totalPaid + '</td>' +
                        '<td class="center-align">' + l.totalPaidTax + '</td>' +
                        '<td class="center-align">' + l.arrearsAmount  + '</td>' +
                        '<td class="center-align">' + l.arrearsTax + '</td>' +
                        '<td class="center-align">'+ a_links +'</td>' +
                        '</tr>';
            }
        } else {
            opt += '<tr>' +
                    '<td colspan="9" class="center-align">' +
                    $('#no-record').val() +
                    '</td>' +
                    '</tr>';
        }

        opt += '</tbody>';
//        if (landInfo.length > 1) {
//            opt += initTableFoot(total);
//        }
        $('.land-table').html(opt);

    }

    function initTableHead() {
        var opt = '';
        opt += '<thead>' +
                '<tr class="table-header">' +
                '<th class="center-align">' + $('#file_no').val() + '</th>' +
                '<th class="center-align">' + $('#lease_no').val() + '</th>' +
                '<th class="center-align">' + $('#land_valuation').val() + '</th>' +
                '<th class="center-align">' + $('#annual_rental').val() + '</th>' +
                '<th class="center-align">' + $('#tot_paid_amount').val() + '</th>' +
                '<th class="center-align">' + $('#tot_paid_tax').val() + '</th>' +
                '<th class="center-align">' + $('#tot_arrears_amount').val() + '</th>' +
                '<th class="center-align">' + $('#tot_arrears_tax').val() + '</th>' +
                '<th class="center-align">' + $('#actions').val() + '</th>' +
                '</tr>' +
                '</thead>';
        return opt;
    }

    function initTableFoot(total) {
        var opt = '';
        opt += '<tfoot>' +
                '<tr class="table-header">' +
                '<th class="center-align">' + $('#all_lands_sum').val() + '</th>' +
                '<th class="center-align">' + total + '</th>' +
                '</tr>' +
                '</tfoot>';
        return opt;
    }


</script>

<div id="land-registration-module-home-title" class="module-home-title">
    <s:a href="reportModule.do">
        <span><img src="${pageContext.request.contextPath}/images/print.png" alt="Print">
            <s:label value="%{getText('reports.label')}"/></span>
    </s:a>
    <span class="module-title-breaker"> > </span>
    <s:a href="leaseArrearsReport.do">
        <span><img src="${pageContext.request.contextPath}/images/list.png" alt="">
            <s:label value="Lease Arrears Report"/></span>
    </s:a>
    <div id="back-button" align="right">
        <s:form><s:submit value="%{getText('back_btn.label')}" action="reportModule"/></s:form>
    </div>
</div>

<div id="printbtn" class="form-button-div right-align">
    <input type="button" style="margin:10" id="printBtnId" value="Print" onclick="printPage('init-land-report-list', 'A4', 'P');"/>
</div>

<div id="init-land-report-list">
    <div class="page-title center-align">
        <h2><s:label value="Lease Arrears Report"/></h2>
    </div>
    <table class="width-635-px margin-10-auto" id="filter-lists">
        <tr>
            <td colspan="4" class="left-align">
                <s:label value="Branch"/>
            </td>
        </tr>
        <tr>
            <td colspan="4" class="left-align">
                <s:if test="%{branchList.size==1}">
                    <s:select id="branchId" list="branchList" name="branchId">
                    </s:select>
                </s:if>
                <s:else>
                    <s:select id="branchId" headerKey="0"
                              headerValue="%{getText('all.label')}" list="branchList" name="branchId">
                    </s:select>
                </s:else>

            </td>
        </tr>

        <tr>
            <td colspan="2" class="left-align">
                <s:label value="Railway Line"/>
            </td>
            <td colspan="2">
                <s:label value="%{getText('district.label')}"/>
            </td>
        </tr>
        <tr>
            <td colspan="2" class="left-align">
                <s:if test="%{railwayTrackList.size == 1}">
                    <s:select id="railwayTrackId" name='railwayTrackId' list='railwayTrackList'>
                    </s:select>
                </s:if>
                <s:else>
                    <s:select id="railwayTrackId" headerKey="0"
                              headerValue="%{getText('all.label')}" name='railwayTrackId' list='railwayTrackList'>
                    </s:select>
                </s:else>
            </td>

            <td colspan="2" class="left-align">
                <select id="districtId" name="districtName" class="width-250-px"><option value="0"><s:property value="%{getText('all.label')}"/></option></select>
                <%--<s:if test="%{districtList.size == 1}">--%>
                    <%--<s:select id="districtId" list ='districtList' name='districtId'>--%>
                    <%--</s:select>--%>
                <%--</s:if>--%>
                <%--<s:else>--%>
                    <%--<s:select id="districtId" headerKey="0"--%>
                              <%--headerValue="%{getText('all.label')}" list ='districtList' name='districtId'>--%>
                    <%--</s:select>--%>
                <%--</s:else>--%>
            </td>
        </tr>
        <tr>
           <td colspan="4" class="left-align">Arrears Amount Range</td>
        </tr>
        <tr>
           <td>From (Rs)</td>
           <td class="left-align">
               <s:textfield id="arrearsMinId"  name="arrearsMin" onKeyPress="return numbersonly(event,true)" />
           </td>
           <td>To (Rs)</td>
           <td class="left-align">
               <s:textfield id="arrearsMaxId" name="arrearsMax" onKeyPress="return numbersonly(event,true)"/>
           </td>
        </tr>

        <tr>
            <td colspan="4" class="right-align margin-top-30px" >
                <s:submit value="Submit Query" onClick="generateStaicstics()"></s:submit>
            </td>
        </tr>
        </table>


<table id="land-table" class="width-100 margin-10-auto margin-top-30px  table land-table" >
</table>
</div>

<s:hidden id="display" value="%{getText('display.label')}"/>
<s:hidden id="records" value="%{getText('records.label')}"/>
<s:hidden id="search" value="%{getText('search.label')}"/>
<s:hidden id="no-record" value="%{getText('no_record.label')}"/>
<s:hidden id="showing" value="%{getText('showing.label')}"/>
<s:hidden id="to" value="%{getText('to.label')}"/>
<s:hidden id="of" value="%{getText('of.label')}"/>
<s:hidden id="entries" value="%{getText('entries.label')}"/>
<s:hidden id="first" value="%{getText('first.label')}"/>
<s:hidden id="last" value="%{getText('last.label')}"/>
<s:hidden id="next" value="%{getText('next.label')}"/>
<s:hidden id="previous" value="%{getText('previous.label')}"/>
<s:hidden id="all" value="%{getText('all.label')}"/>

<s:hidden id="lease_no" value="%{getText('lease_no.label')}"/>
<s:hidden id="file_no" value="%{getText('file_no.label')}"/>
<s:hidden id="land_valuation" value="%{getText('land_valuation.label')}"/>
<s:hidden id="tot_paid_amount" value="%{getText('tot_paid_amount.label')}"/>
<s:hidden id="annual_rental" value="%{getText('annual_rental.label')}"/>
<s:hidden id="tot_paid_tax" value="%{getText('tot_paid_tax.label')}"/>
<s:hidden id="tot_arrears_amount" value="%{getText('tot_arrears_amount.label')}"/>
<s:hidden id="tot_arrears_tax" value="%{getText('tot_arrears_tax.label')}"/>
<s:hidden id="actions" value="Action"/>